package com.geek.water.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.geek.water.constant.CodeConstant;
import com.geek.water.constant.MessageConstant;
import com.geek.water.entity.AlertLog;
import com.geek.water.result.Result;
import com.geek.water.service.AlertLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author yangwenqi
 * @decs 告警日志控制器
 * @date 2025/1/2
 */
@Slf4j
@RestController
@RequestMapping("/api/alert-logs")
@Api(tags = "告警日志管理")
public class AlertLogController {

    private final AlertLogService alertLogService;

    @Autowired
    public AlertLogController(AlertLogService alertLogService) {
        this.alertLogService = alertLogService;
    }

    @PostMapping("/save")
    @ApiOperation("保存告警日志")
    public Result saveAlertLog(@RequestBody AlertLog alertLog) {
        try {
            boolean result = alertLogService.save(alertLog);
            return result ? new Result(CodeConstant.SUCCESS, "保存成功") : new Result(CodeConstant.FAIL, "保存失败");
        } catch (Exception e) {
            log.error("保存告警日志失败", e);
            return new Result(CodeConstant.FAIL, "保存告警日志失败：" + e.getMessage());
        }
    }

    @PostMapping("/update")
    @ApiOperation("更新告警日志")
    public Result updateAlertLog(@RequestBody AlertLog alertLog) {
        try {
            boolean result = alertLogService.updateById(alertLog);
            return result ? new Result(CodeConstant.SUCCESS, "更新成功") : new Result(CodeConstant.FAIL, "更新失败");
        } catch (Exception e) {
            log.error("更新告警日志失败", e);
            return new Result(CodeConstant.FAIL, "更新告警日志失败：" + e.getMessage());
        }
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除告警日志")
    public Result deleteAlertLog(@PathVariable Long id) {
        try {
            boolean result = alertLogService.removeById(id);
            return result ? new Result(CodeConstant.SUCCESS, "删除成功") : new Result(CodeConstant.FAIL, "删除失败");
        } catch (Exception e) {
            log.error("删除告警日志失败", e);
            return new Result(CodeConstant.FAIL, "删除告警日志失败：" + e.getMessage());
        }
    }

    @GetMapping("/{id}")
    @ApiOperation("根据ID获取告警日志")
    public Result getAlertLogById(@PathVariable Long id) {
        try {
            AlertLog alertLog = alertLogService.getById(id);
            return alertLog != null ? new Result(CodeConstant.SUCCESS, "查询成功", alertLog) : new Result(CodeConstant.FAIL, "告警日志不存在");
        } catch (Exception e) {
            log.error("获取告警日志失败", e);
            return new Result(CodeConstant.FAIL, "获取告警日志失败：" + e.getMessage());
        }
    }

    @GetMapping("/type/{type}")
    @ApiOperation("根据类型查询告警日志")
    public Result getAlertLogsByType(@PathVariable String type) {
        try {
            LambdaQueryWrapper<AlertLog> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(AlertLog::getType, type)
                    .orderByDesc(AlertLog::getCreatedAt);
            List<AlertLog> alertLogs = alertLogService.list(queryWrapper);
            return new Result(CodeConstant.SUCCESS, "查询成功", alertLogs);
        } catch (Exception e) {
            log.error("查询告警日志失败", e);
            return new Result(CodeConstant.FAIL, "查询告警日志失败：" + e.getMessage());
        }
    }

    @GetMapping("/entity/{entityId}")
    @ApiOperation("根据实体ID查询告警日志")
    public Result getAlertLogsByEntityId(@PathVariable Long entityId) {
        try {
            LambdaQueryWrapper<AlertLog> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(AlertLog::getEntityId, entityId)
                    .orderByDesc(AlertLog::getCreatedAt);
            List<AlertLog> alertLogs = alertLogService.list(queryWrapper);
            return new Result(CodeConstant.SUCCESS, "查询成功", alertLogs);
        } catch (Exception e) {
            log.error("查询告警日志失败", e);
            return new Result(CodeConstant.FAIL, "查询告警日志失败：" + e.getMessage());
        }
    }

    @GetMapping("/list")
    @ApiOperation("分页查询告警日志")
    public Result listAlertLogs(@RequestParam(defaultValue = "1") Integer currentPage,
                                @RequestParam(defaultValue = "10") Integer pageSize,
                                @RequestParam(required = false) String type,
                                @RequestParam(required = false) Long entityId) {
        try {
            Page<AlertLog> page = new Page<>(currentPage, pageSize);

            LambdaQueryWrapper<AlertLog> queryWrapper = new LambdaQueryWrapper<>();
            if (type != null) {
                queryWrapper.eq(AlertLog::getType, type);
            }
            if (entityId != null) {
                queryWrapper.eq(AlertLog::getEntityId, entityId);
            }
            queryWrapper.orderByDesc(AlertLog::getCreatedAt);

            Page<AlertLog> result = alertLogService.page(page, queryWrapper);
            return new Result(CodeConstant.SUCCESS, "查询成功", result);
        } catch (Exception e) {
            log.error("查询告警日志失败", e);
            return new Result(CodeConstant.FAIL, "查询告警日志失败：" + e.getMessage());
        }
    }
}
